﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class admin_qlcategory : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#99ffcc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        e.Cancel = true;
        Label id = (Label)this.GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("id");
        Response.Redirect("Default.aspx?page=edcategory&id=" + id.Text);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.CompareTo("addlang") == 0)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            Label id = (Label)row.FindControl("id");
            Response.Redirect("Default.aspx?page=qlcategorylang&id=" + id.Text);
        }
    }

    protected void addbt_Click(object sender, EventArgs e)
    {
        try
        {
            connection con = new connection();
            SqlConnection cn = con.connect();
            SqlCommand mySqlSelect = new SqlCommand("insert into category values(@id, @url, @pro, @des)", cn);
            mySqlSelect.CommandType = CommandType.Text;

            SqlParameter desc = new SqlParameter("@des", SqlDbType.NVarChar, 150);
            desc.Value = des.Text;

            SqlParameter id = new SqlParameter("@id", SqlDbType.BigInt);
            Double idt = Common.ToSeconds(DateTime.Now);
            id.Value = idt;

            SqlParameter pro = new SqlParameter("@pro", SqlDbType.Bit);
            if (isPro.Checked)
            {
                pro.Value = 1;
            }
            else
            {
                pro.Value = 0;
            }

            SqlParameter url = new SqlParameter("@url", SqlDbType.NVarChar, 150);
            String regex = "";
            if (img.PostedFile.ContentLength > 2097151)
            {
                info.Visible = true;
                info.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
                return;
            }
            else
            {
                regex = System.IO.Path.GetExtension(img.PostedFile.FileName.ToLower().ToString());
                String picPath = "~/image/" + idt + regex;
                if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
                {
                    Common.resizeImage(Server.MapPath(picPath), 350, 225, img);
                }
                else
                {
                    info.Visible = true;
                    info.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
                }
            }

            if (regex == "")
            {
                url.Value = DBNull.Value;
            }
            else
            {
                url.Value = idt + regex;
            }

            mySqlSelect.Parameters.Add(desc);
            mySqlSelect.Parameters.Add(id);
            mySqlSelect.Parameters.Add(pro);
            mySqlSelect.Parameters.Add(url);
            mySqlSelect.ExecuteNonQuery();
            cn.Close();
            SqlDataSource1.DataBind();
            GridView1.DataBind();
            info.Text = "Thêm dữ liệu thành công";
        }
        catch (Exception ex)
        {
            info.Text = "Có lỗi xảy ra trong quá trình nhập liệu";
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        e.Cancel = true;
        Common.confirmDelete(null);
        //Response.Write(Common.confirmDelete(null));
        Label id = (Label)this.GridView1.Rows[e.RowIndex].Cells[0].FindControl("id");
        Label lang = (Label)this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("lg");
        connection con = new connection();
        SqlConnection cn = con.connect();
        //xoa category_lang
        SqlCommand mySqlSelect = new SqlCommand("delete from category_lang where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter idt = new SqlParameter("@id", SqlDbType.BigInt);
        idt.Value = Int64.Parse(id.Text);

        mySqlSelect.Parameters.Add(idt);
        mySqlSelect.ExecuteNonQuery();
        //xoa category
        SqlCommand mySqlSelect1 = new SqlCommand("delete from category where id = @idtt", cn);
        mySqlSelect1.CommandType = CommandType.Text;

        SqlParameter idtt = new SqlParameter("@idtt", SqlDbType.BigInt);
        idtt.Value = Int64.Parse(id.Text);

        mySqlSelect1.Parameters.Add(idtt);
        mySqlSelect1.ExecuteNonQuery();

        cn.Close();
        SqlDataSource1.DataBind();
        GridView1.DataBind();
    }
}